﻿//===============================================================================
// Microsoft Innovation Centre - Western Europe
// Copyright (c) 2008 Microsoft Corporation. All rights reserved.
//
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY
// OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT
// LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
// FITNESS FOR A PARTICULAR PURPOSE.
//===============================================================================
// The example companies, organizations, products, domain names,
// e-mail addresses, logos, people, places, and events depicted
// herein are fictitious.  No association with any real company,
// organization, product, domain name, email address, logo, person,
// places, or events is intended or should be inferred.
//===============================================================================

using System;
using System.Windows;
using System.Windows.Controls;
using Microsoft.WVB.Silverlight.Infrastructure;

namespace Microsoft.WVB.Framework
{
    /// <summary>
    /// 
    /// </summary>
    public static class AttachAdService
    {
        public static object GetLayout(DependencyObject element)
        {
            if (element == null)
            {
                throw new ArgumentNullException("element");
            }
            return element.GetValue(LayoutProperty);
        }

        public static void SetLayout(DependencyObject element, object value)
        {
            if (element == null)
            {
                throw new ArgumentNullException("element");
            }
            element.SetValue(LayoutProperty, value);
        }

        public static readonly DependencyProperty LayoutProperty = null;

        static AttachAdService()
        {
            LayoutProperty = DependencyProperty.RegisterAttached("Layout", typeof(AdVisual), typeof(AttachAdService), new PropertyMetadata(AdVisual.Banner, new PropertyChangedCallback(OnLayoutChanged)));
        }

        static void OnLayoutChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            // Verify the validity of the object captured ...
            AdManager adManager = null;
            Panel panel = d as Panel;

            if (panel == null)
                throw new ArgumentOutOfRangeException("d", "AttachAdService must be attached to a Panel-derived control");
           
            //object obj = Enum.Parse(typeof(AdVisual), (string)e.NewValue, true);
            AdVisual mode = (AdVisual) e.NewValue;

            // Call the AdManager to register the Panel and the Type of Visual for that Panel
            if (!System.ComponentModel.DesignerProperties.GetIsInDesignMode(d))
            {
                adManager = IoC.GetCreate<AdManager>();
            }

            if (adManager != null)
            {
                adManager.RegisterAdDisplaySurface(mode, d as Panel);
                //adManager.RegisterAdDisplaySurface((AdVisual)obj, d as Panel);
            }
        }
    }
}
